<html><!-- Created using the cpp_pretty_printer from the dlib C++ library.  See http://dlib.net for updates. --><head><title>dlib C++ Library - find_max_factor_graph_potts.h</title></head><body bgcolor='white'><pre>
<font color='#009900'>// Copyright (C) 2012  Davis E. King (davis@dlib.net)
</font><font color='#009900'>// License: Boost Software License   See LICENSE.txt for the full license.
</font><font color='#0000FF'>#ifndef</font> DLIB_FIND_MAX_FACTOR_GRAPH_PoTTS_Hh_
<font color='#0000FF'>#define</font> DLIB_FIND_MAX_FACTOR_GRAPH_PoTTS_Hh_

<font color='#0000FF'>#include</font> "<a style='text-decoration:none' href='find_max_factor_graph_potts_abstract.h.html'>find_max_factor_graph_potts_abstract.h</a>"
<font color='#0000FF'>#include</font> "<a style='text-decoration:none' href='../matrix.h.html'>../matrix.h</a>"
<font color='#0000FF'>#include</font> "<a style='text-decoration:none' href='min_cut.h.html'>min_cut.h</a>"
<font color='#0000FF'>#include</font> "<a style='text-decoration:none' href='general_potts_problem.h.html'>general_potts_problem.h</a>"
<font color='#0000FF'>#include</font> "<a style='text-decoration:none' href='../algs.h.html'>../algs.h</a>"
<font color='#0000FF'>#include</font> "<a style='text-decoration:none' href='../graph_utils.h.html'>../graph_utils.h</a>"
<font color='#0000FF'>#include</font> "<a style='text-decoration:none' href='../array2d.h.html'>../array2d.h</a>"

<font color='#0000FF'>namespace</font> dlib
<b>{</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    <font color='#0000FF'>namespace</font> impl
    <b>{</b>

        <font color='#0000FF'>template</font> <font color='#5555FF'>&lt;</font>
            <font color='#0000FF'>typename</font> potts_problem,
            <font color='#0000FF'>typename</font> T <font color='#5555FF'>=</font> <font color='#0000FF'><u>void</u></font>
            <font color='#5555FF'>&gt;</font>
        <font color='#0000FF'>class</font> <b><a name='flows_container'></a>flows_container</b>
        <b>{</b>
            <font color='#009900'>/*
                This object notionally represents a matrix of flow values.  It's
                overloaded to represent this matrix efficiently though.  In this case
                it represents the matrix using a sparse representation.
            */</font>

            <font color='#0000FF'>typedef</font> <font color='#0000FF'>typename</font> potts_problem::value_type edge_type;
            std::vector<font color='#5555FF'>&lt;</font>std::vector<font color='#5555FF'>&lt;</font>edge_type<font color='#5555FF'>&gt;</font> <font color='#5555FF'>&gt;</font> flows;
        <font color='#0000FF'>public</font>:

            <font color='#0000FF'><u>void</u></font> <b><a name='setup'></a>setup</b><font face='Lucida Console'>(</font>
                <font color='#0000FF'>const</font> potts_problem<font color='#5555FF'>&amp;</font> p
            <font face='Lucida Console'>)</font>
            <b>{</b>
                flows.<font color='#BB00BB'>resize</font><font face='Lucida Console'>(</font>p.<font color='#BB00BB'>number_of_nodes</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
                <font color='#0000FF'>for</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> i <font color='#5555FF'>=</font> <font color='#979000'>0</font>; i <font color='#5555FF'>&lt;</font> flows.<font color='#BB00BB'>size</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>; <font color='#5555FF'>+</font><font color='#5555FF'>+</font>i<font face='Lucida Console'>)</font>
                <b>{</b>
                    flows[i].<font color='#BB00BB'>resize</font><font face='Lucida Console'>(</font>p.<font color='#BB00BB'>number_of_neighbors</font><font face='Lucida Console'>(</font>i<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
                <b>}</b>
            <b>}</b>

            edge_type<font color='#5555FF'>&amp;</font> <b><a name='operator'></a>operator</b><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font face='Lucida Console'>(</font>
                <font color='#0000FF'>const</font> <font color='#0000FF'><u>long</u></font> r,
                <font color='#0000FF'>const</font> <font color='#0000FF'><u>long</u></font> c
            <font face='Lucida Console'>)</font> <b>{</b> <font color='#0000FF'>return</font> flows[r][c]; <b>}</b>

            <font color='#0000FF'>const</font> edge_type<font color='#5555FF'>&amp;</font> <b><a name='operator'></a>operator</b><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font face='Lucida Console'>(</font>
                <font color='#0000FF'>const</font> <font color='#0000FF'><u>long</u></font> r,
                <font color='#0000FF'>const</font> <font color='#0000FF'><u>long</u></font> c
            <font face='Lucida Console'>)</font> <font color='#0000FF'>const</font> <b>{</b> <font color='#0000FF'>return</font> flows[r][c]; <b>}</b>
        <b>}</b>;

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
        <font color='#0000FF'>template</font> <font color='#5555FF'>&lt;</font>
            <font color='#0000FF'>typename</font> potts_problem
            <font color='#5555FF'>&gt;</font>
        <font color='#0000FF'>class</font> <b><a name='flows_container'></a>flows_container</b><font color='#5555FF'>&lt;</font>potts_problem, 
                              <font color='#0000FF'>typename</font> enable_if_c<font color='#5555FF'>&lt;</font>potts_problem::max_number_of_neighbors<font color='#5555FF'>!</font><font color='#5555FF'>=</font><font color='#979000'>0</font><font color='#5555FF'>&gt;</font>::type<font color='#5555FF'>&gt;</font>
        <b>{</b>
            <font color='#009900'>/*
                This object notionally represents a matrix of flow values.  It's
                overloaded to represent this matrix efficiently though.  In this case
                it represents the matrix using a dense representation.

            */</font>
            <font color='#0000FF'>typedef</font> <font color='#0000FF'>typename</font> potts_problem::value_type edge_type;
            <font color='#0000FF'>const</font> <font color='#0000FF'>static</font> <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> max_number_of_neighbors <font color='#5555FF'>=</font> potts_problem::max_number_of_neighbors;
            matrix<font color='#5555FF'>&lt;</font>edge_type,<font color='#979000'>0</font>,max_number_of_neighbors<font color='#5555FF'>&gt;</font> flows;
        <font color='#0000FF'>public</font>:

            <font color='#0000FF'><u>void</u></font> <b><a name='setup'></a>setup</b><font face='Lucida Console'>(</font>
                <font color='#0000FF'>const</font> potts_problem<font color='#5555FF'>&amp;</font> p
            <font face='Lucida Console'>)</font>
            <b>{</b>
                flows.<font color='#BB00BB'>set_size</font><font face='Lucida Console'>(</font>p.<font color='#BB00BB'>number_of_nodes</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>, max_number_of_neighbors<font face='Lucida Console'>)</font>;
            <b>}</b>

            edge_type<font color='#5555FF'>&amp;</font> <b><a name='operator'></a>operator</b><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font face='Lucida Console'>(</font>
                <font color='#0000FF'>const</font> <font color='#0000FF'><u>long</u></font> r,
                <font color='#0000FF'>const</font> <font color='#0000FF'><u>long</u></font> c
            <font face='Lucida Console'>)</font> <b>{</b> <font color='#0000FF'>return</font> <font color='#BB00BB'>flows</font><font face='Lucida Console'>(</font>r,c<font face='Lucida Console'>)</font>; <b>}</b>

            <font color='#0000FF'>const</font> edge_type<font color='#5555FF'>&amp;</font> <b><a name='operator'></a>operator</b><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font face='Lucida Console'>(</font>
                <font color='#0000FF'>const</font> <font color='#0000FF'><u>long</u></font> r,
                <font color='#0000FF'>const</font> <font color='#0000FF'><u>long</u></font> c
            <font face='Lucida Console'>)</font> <font color='#0000FF'>const</font> <b>{</b> <font color='#0000FF'>return</font> <font color='#BB00BB'>flows</font><font face='Lucida Console'>(</font>r,c<font face='Lucida Console'>)</font>; <b>}</b>
        <b>}</b>;

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
        <font color='#0000FF'>template</font> <font color='#5555FF'>&lt;</font>
            <font color='#0000FF'>typename</font> potts_problem 
            <font color='#5555FF'>&gt;</font>
        <font color='#0000FF'>class</font> <b><a name='potts_flow_graph'></a>potts_flow_graph</b> 
        <b>{</b>
        <font color='#0000FF'>public</font>:
            <font color='#0000FF'>typedef</font> <font color='#0000FF'>typename</font> potts_problem::value_type edge_type;
        <font color='#0000FF'>private</font>:
            <font color='#009900'>/*!
                This is a utility class used by dlib::min_cut to convert a potts_problem 
                into the kind of flow graph expected by the min_cut object's main block
                of code.

                Within this object, we will use the convention that one past 
                potts_problem::number_of_nodes() is the source node and two past is 
                the sink node.
            !*/</font>

            potts_problem<font color='#5555FF'>&amp;</font> g;

            <font color='#009900'>// flows(i,j) == the flow from node id i to it's jth neighbor
</font>            flows_container<font color='#5555FF'>&lt;</font>potts_problem<font color='#5555FF'>&gt;</font> flows;
            <font color='#009900'>// source_flows(i,0) == flow from source to node i, 
</font>            <font color='#009900'>// source_flows(i,1) == flow from node i to source
</font>            matrix<font color='#5555FF'>&lt;</font>edge_type,<font color='#979000'>0</font>,<font color='#979000'>2</font><font color='#5555FF'>&gt;</font> source_flows;

            <font color='#009900'>// sink_flows(i,0) == flow from sink to node i, 
</font>            <font color='#009900'>// sink_flows(i,1) == flow from node i to sink
</font>            matrix<font color='#5555FF'>&lt;</font>edge_type,<font color='#979000'>0</font>,<font color='#979000'>2</font><font color='#5555FF'>&gt;</font> sink_flows;

            node_label source_label, sink_label;
        <font color='#0000FF'>public</font>:

            <b><a name='potts_flow_graph'></a>potts_flow_graph</b><font face='Lucida Console'>(</font>
                potts_problem<font color='#5555FF'>&amp;</font> g_
            <font face='Lucida Console'>)</font> : g<font face='Lucida Console'>(</font>g_<font face='Lucida Console'>)</font>
            <b>{</b>
                flows.<font color='#BB00BB'>setup</font><font face='Lucida Console'>(</font>g<font face='Lucida Console'>)</font>;

                source_flows.<font color='#BB00BB'>set_size</font><font face='Lucida Console'>(</font>g.<font color='#BB00BB'>number_of_nodes</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>, <font color='#979000'>2</font><font face='Lucida Console'>)</font>;
                sink_flows.<font color='#BB00BB'>set_size</font><font face='Lucida Console'>(</font>g.<font color='#BB00BB'>number_of_nodes</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>, <font color='#979000'>2</font><font face='Lucida Console'>)</font>;
                source_flows <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
                sink_flows <font color='#5555FF'>=</font> <font color='#979000'>0</font>;

                source_label <font color='#5555FF'>=</font> FREE_NODE;
                sink_label <font color='#5555FF'>=</font> FREE_NODE;

                <font color='#009900'>// setup flows based on factor potentials
</font>                <font color='#0000FF'>for</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> i <font color='#5555FF'>=</font> <font color='#979000'>0</font>; i <font color='#5555FF'>&lt;</font> g.<font color='#BB00BB'>number_of_nodes</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>; <font color='#5555FF'>+</font><font color='#5555FF'>+</font>i<font face='Lucida Console'>)</font>
                <b>{</b>
                    <font color='#0000FF'>const</font> edge_type temp <font color='#5555FF'>=</font> g.<font color='#BB00BB'>factor_value</font><font face='Lucida Console'>(</font>i<font face='Lucida Console'>)</font>;
                    <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>temp <font color='#5555FF'>&lt;</font> <font color='#979000'>0</font><font face='Lucida Console'>)</font>
                        <font color='#BB00BB'>source_flows</font><font face='Lucida Console'>(</font>i,<font color='#979000'>0</font><font face='Lucida Console'>)</font> <font color='#5555FF'>=</font> <font color='#5555FF'>-</font>temp;
                    <font color='#0000FF'>else</font>
                        <font color='#BB00BB'>sink_flows</font><font face='Lucida Console'>(</font>i,<font color='#979000'>1</font><font face='Lucida Console'>)</font> <font color='#5555FF'>=</font> temp;

                    <font color='#0000FF'>for</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> j <font color='#5555FF'>=</font> <font color='#979000'>0</font>; j <font color='#5555FF'>&lt;</font> g.<font color='#BB00BB'>number_of_neighbors</font><font face='Lucida Console'>(</font>i<font face='Lucida Console'>)</font>; <font color='#5555FF'>+</font><font color='#5555FF'>+</font>j<font face='Lucida Console'>)</font>
                    <b>{</b>
                        <font color='#BB00BB'>flows</font><font face='Lucida Console'>(</font>i,j<font face='Lucida Console'>)</font> <font color='#5555FF'>=</font> g.<font color='#BB00BB'>factor_value_disagreement</font><font face='Lucida Console'>(</font>i, g.<font color='#BB00BB'>get_neighbor</font><font face='Lucida Console'>(</font>i,j<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
                    <b>}</b>
                <b>}</b>
            <b>}</b>

            <font color='#0000FF'>class</font> <b><a name='out_edge_iterator'></a>out_edge_iterator</b>
            <b>{</b>
                <font color='#0000FF'>friend</font> <font color='#0000FF'>class</font> potts_flow_graph;
                <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> idx; <font color='#009900'>// base node idx
</font>                <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> cnt; <font color='#009900'>// count over the neighbors of idx
</font>            <font color='#0000FF'>public</font>:

                <b><a name='out_edge_iterator'></a>out_edge_iterator</b><font face='Lucida Console'>(</font>
                <font face='Lucida Console'>)</font>:idx<font face='Lucida Console'>(</font><font color='#979000'>0</font><font face='Lucida Console'>)</font>,cnt<font face='Lucida Console'>(</font><font color='#979000'>0</font><font face='Lucida Console'>)</font><b>{</b><b>}</b>

                <b><a name='out_edge_iterator'></a>out_edge_iterator</b><font face='Lucida Console'>(</font>
                    <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> idx_,
                    <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> cnt_
                <font face='Lucida Console'>)</font>:idx<font face='Lucida Console'>(</font>idx_<font face='Lucida Console'>)</font>,cnt<font face='Lucida Console'>(</font>cnt_<font face='Lucida Console'>)</font>
                <b>{</b><b>}</b>

                <font color='#0000FF'><u>bool</u></font> <b><a name='operator'></a>operator</b><font color='#5555FF'>!</font><font color='#5555FF'>=</font> <font face='Lucida Console'>(</font>
                    <font color='#0000FF'>const</font> out_edge_iterator<font color='#5555FF'>&amp;</font> item
                <font face='Lucida Console'>)</font> <font color='#0000FF'>const</font> <b>{</b> <font color='#0000FF'>return</font> cnt <font color='#5555FF'>!</font><font color='#5555FF'>=</font> item.cnt; <b>}</b>

                out_edge_iterator<font color='#5555FF'>&amp;</font> <b><a name='operator'></a>operator</b><font color='#5555FF'>+</font><font color='#5555FF'>+</font><font face='Lucida Console'>(</font>
                <font face='Lucida Console'>)</font>
                <b>{</b>
                    <font color='#5555FF'>+</font><font color='#5555FF'>+</font>cnt;
                    <font color='#0000FF'>return</font> <font color='#5555FF'>*</font><font color='#0000FF'>this</font>;
                <b>}</b>
            <b>}</b>;

            <font color='#0000FF'>class</font> <b><a name='in_edge_iterator'></a>in_edge_iterator</b>
            <b>{</b>
                <font color='#0000FF'>friend</font> <font color='#0000FF'>class</font> potts_flow_graph;
                <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> idx; <font color='#009900'>// base node idx
</font>                <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> cnt; <font color='#009900'>// count over the neighbors of idx
</font>            <font color='#0000FF'>public</font>:

                <b><a name='in_edge_iterator'></a>in_edge_iterator</b><font face='Lucida Console'>(</font>
                <font face='Lucida Console'>)</font>:idx<font face='Lucida Console'>(</font><font color='#979000'>0</font><font face='Lucida Console'>)</font>,cnt<font face='Lucida Console'>(</font><font color='#979000'>0</font><font face='Lucida Console'>)</font>  
                <b>{</b><b>}</b>


                <b><a name='in_edge_iterator'></a>in_edge_iterator</b><font face='Lucida Console'>(</font>
                    <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> idx_,
                    <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> cnt_
                <font face='Lucida Console'>)</font>:idx<font face='Lucida Console'>(</font>idx_<font face='Lucida Console'>)</font>,cnt<font face='Lucida Console'>(</font>cnt_<font face='Lucida Console'>)</font>
                <b>{</b><b>}</b>

                <font color='#0000FF'><u>bool</u></font> <b><a name='operator'></a>operator</b><font color='#5555FF'>!</font><font color='#5555FF'>=</font> <font face='Lucida Console'>(</font>
                    <font color='#0000FF'>const</font> in_edge_iterator<font color='#5555FF'>&amp;</font> item
                <font face='Lucida Console'>)</font> <font color='#0000FF'>const</font> <b>{</b> <font color='#0000FF'>return</font> cnt <font color='#5555FF'>!</font><font color='#5555FF'>=</font> item.cnt; <b>}</b>

                in_edge_iterator<font color='#5555FF'>&amp;</font> <b><a name='operator'></a>operator</b><font color='#5555FF'>+</font><font color='#5555FF'>+</font><font face='Lucida Console'>(</font>
                <font face='Lucida Console'>)</font>
                <b>{</b>
                    <font color='#5555FF'>+</font><font color='#5555FF'>+</font>cnt;
                    <font color='#0000FF'>return</font> <font color='#5555FF'>*</font><font color='#0000FF'>this</font>;
                <b>}</b>
            <b>}</b>;

            <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> <b><a name='number_of_nodes'></a>number_of_nodes</b> <font face='Lucida Console'>(</font>
            <font face='Lucida Console'>)</font> <font color='#0000FF'>const</font> <b>{</b> <font color='#0000FF'>return</font> g.<font color='#BB00BB'>number_of_nodes</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>+</font> <font color='#979000'>2</font>; <b>}</b>

            out_edge_iterator <b><a name='out_begin'></a>out_begin</b><font face='Lucida Console'>(</font>
                <font color='#0000FF'>const</font> <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font><font color='#5555FF'>&amp;</font> it
            <font face='Lucida Console'>)</font> <font color='#0000FF'>const</font> <b>{</b> <font color='#0000FF'>return</font> <font color='#BB00BB'>out_edge_iterator</font><font face='Lucida Console'>(</font>it, <font color='#979000'>0</font><font face='Lucida Console'>)</font>; <b>}</b>

            in_edge_iterator <b><a name='in_begin'></a>in_begin</b><font face='Lucida Console'>(</font>
                <font color='#0000FF'>const</font> <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font><font color='#5555FF'>&amp;</font> it
            <font face='Lucida Console'>)</font> <font color='#0000FF'>const</font> <b>{</b> <font color='#0000FF'>return</font> <font color='#BB00BB'>in_edge_iterator</font><font face='Lucida Console'>(</font>it, <font color='#979000'>0</font><font face='Lucida Console'>)</font>; <b>}</b>

            out_edge_iterator <b><a name='out_end'></a>out_end</b><font face='Lucida Console'>(</font>
                <font color='#0000FF'>const</font> <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font><font color='#5555FF'>&amp;</font> it
            <font face='Lucida Console'>)</font> <font color='#0000FF'>const</font> 
            <b>{</b> 
                <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>it <font color='#5555FF'>&gt;</font><font color='#5555FF'>=</font> g.<font color='#BB00BB'>number_of_nodes</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>
                    <font color='#0000FF'>return</font> <font color='#BB00BB'>out_edge_iterator</font><font face='Lucida Console'>(</font>it, g.<font color='#BB00BB'>number_of_nodes</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>; 
                <font color='#0000FF'>else</font>
                    <font color='#0000FF'>return</font> <font color='#BB00BB'>out_edge_iterator</font><font face='Lucida Console'>(</font>it, g.<font color='#BB00BB'>number_of_neighbors</font><font face='Lucida Console'>(</font>it<font face='Lucida Console'>)</font><font color='#5555FF'>+</font><font color='#979000'>2</font><font face='Lucida Console'>)</font>; 
            <b>}</b>

            in_edge_iterator <b><a name='in_end'></a>in_end</b><font face='Lucida Console'>(</font>
                <font color='#0000FF'>const</font> <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font><font color='#5555FF'>&amp;</font> it
            <font face='Lucida Console'>)</font> <font color='#0000FF'>const</font> 
            <b>{</b> 
                <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>it <font color='#5555FF'>&gt;</font><font color='#5555FF'>=</font> g.<font color='#BB00BB'>number_of_nodes</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>
                    <font color='#0000FF'>return</font> <font color='#BB00BB'>in_edge_iterator</font><font face='Lucida Console'>(</font>it, g.<font color='#BB00BB'>number_of_nodes</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>; 
                <font color='#0000FF'>else</font>
                    <font color='#0000FF'>return</font> <font color='#BB00BB'>in_edge_iterator</font><font face='Lucida Console'>(</font>it, g.<font color='#BB00BB'>number_of_neighbors</font><font face='Lucida Console'>(</font>it<font face='Lucida Console'>)</font><font color='#5555FF'>+</font><font color='#979000'>2</font><font face='Lucida Console'>)</font>; 
            <b>}</b>


            <font color='#0000FF'>template</font> <font color='#5555FF'>&lt;</font><font color='#0000FF'>typename</font> iterator_type<font color='#5555FF'>&gt;</font>
            <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> <b><a name='node_id'></a>node_id</b> <font face='Lucida Console'>(</font>
                <font color='#0000FF'>const</font> iterator_type<font color='#5555FF'>&amp;</font> it
            <font face='Lucida Console'>)</font> <font color='#0000FF'>const</font> 
            <b>{</b> 
                <font color='#009900'>// if this isn't an iterator over the source or sink nodes
</font>                <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>it.idx <font color='#5555FF'>&lt;</font> g.<font color='#BB00BB'>number_of_nodes</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>
                <b>{</b>
                    <font color='#0000FF'>const</font> <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> num <font color='#5555FF'>=</font> g.<font color='#BB00BB'>number_of_neighbors</font><font face='Lucida Console'>(</font>it.idx<font face='Lucida Console'>)</font>;
                    <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>it.cnt <font color='#5555FF'>&lt;</font> num<font face='Lucida Console'>)</font>
                        <font color='#0000FF'>return</font> g.<font color='#BB00BB'>get_neighbor</font><font face='Lucida Console'>(</font>it.idx, it.cnt<font face='Lucida Console'>)</font>; 
                    <font color='#0000FF'>else</font> <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>it.cnt <font color='#5555FF'>=</font><font color='#5555FF'>=</font> num<font face='Lucida Console'>)</font>
                        <font color='#0000FF'>return</font> g.<font color='#BB00BB'>number_of_nodes</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
                    <font color='#0000FF'>else</font>
                        <font color='#0000FF'>return</font> g.<font color='#BB00BB'>number_of_nodes</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font color='#5555FF'>+</font><font color='#979000'>1</font>;
                <b>}</b>
                <font color='#0000FF'>else</font>
                <b>{</b>
                    <font color='#0000FF'>return</font> it.cnt;
                <b>}</b>
            <b>}</b>


            edge_type <b><a name='get_flow'></a>get_flow</b> <font face='Lucida Console'>(</font>
                <font color='#0000FF'>const</font> <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font><font color='#5555FF'>&amp;</font> it1,     
                <font color='#0000FF'>const</font> <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font><font color='#5555FF'>&amp;</font> it2
            <font face='Lucida Console'>)</font> <font color='#0000FF'>const</font>
            <b>{</b>
                <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>it1 <font color='#5555FF'>&gt;</font><font color='#5555FF'>=</font> g.<font color='#BB00BB'>number_of_nodes</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>
                <b>{</b>
                    <font color='#009900'>// if it1 is the source
</font>                    <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>it1 <font color='#5555FF'>=</font><font color='#5555FF'>=</font> g.<font color='#BB00BB'>number_of_nodes</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>
                    <b>{</b>
                        <font color='#0000FF'>return</font> <font color='#BB00BB'>source_flows</font><font face='Lucida Console'>(</font>it2,<font color='#979000'>0</font><font face='Lucida Console'>)</font>;
                    <b>}</b>
                    <font color='#0000FF'>else</font> <font color='#009900'>// if it1 is the sink
</font>                    <b>{</b>
                        <font color='#0000FF'>return</font> <font color='#BB00BB'>sink_flows</font><font face='Lucida Console'>(</font>it2,<font color='#979000'>0</font><font face='Lucida Console'>)</font>;
                    <b>}</b>
                <b>}</b>
                <font color='#0000FF'>else</font> <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>it2 <font color='#5555FF'>&gt;</font><font color='#5555FF'>=</font> g.<font color='#BB00BB'>number_of_nodes</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>
                <b>{</b>
                    <font color='#009900'>// if it2 is the source
</font>                    <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>it2 <font color='#5555FF'>=</font><font color='#5555FF'>=</font> g.<font color='#BB00BB'>number_of_nodes</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>
                    <b>{</b>
                        <font color='#0000FF'>return</font> <font color='#BB00BB'>source_flows</font><font face='Lucida Console'>(</font>it1,<font color='#979000'>1</font><font face='Lucida Console'>)</font>;
                    <b>}</b>
                    <font color='#0000FF'>else</font> <font color='#009900'>// if it2 is the sink
</font>                    <b>{</b>
                        <font color='#0000FF'>return</font> <font color='#BB00BB'>sink_flows</font><font face='Lucida Console'>(</font>it1,<font color='#979000'>1</font><font face='Lucida Console'>)</font>;
                    <b>}</b>
                <b>}</b>
                <font color='#0000FF'>else</font>
                <b>{</b>
                    <font color='#0000FF'>return</font> <font color='#BB00BB'>flows</font><font face='Lucida Console'>(</font>it1, g.<font color='#BB00BB'>get_neighbor_idx</font><font face='Lucida Console'>(</font>it1, it2<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
                <b>}</b>

            <b>}</b>

            edge_type <b><a name='get_flow'></a>get_flow</b> <font face='Lucida Console'>(</font>
                <font color='#0000FF'>const</font> out_edge_iterator<font color='#5555FF'>&amp;</font> it
            <font face='Lucida Console'>)</font> <font color='#0000FF'>const</font>
            <b>{</b>
                <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>it.idx <font color='#5555FF'>&lt;</font> g.<font color='#BB00BB'>number_of_nodes</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>
                <b>{</b>
                    <font color='#0000FF'>const</font> <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> num <font color='#5555FF'>=</font> g.<font color='#BB00BB'>number_of_neighbors</font><font face='Lucida Console'>(</font>it.idx<font face='Lucida Console'>)</font>;
                    <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>it.cnt <font color='#5555FF'>&lt;</font> num<font face='Lucida Console'>)</font>
                        <font color='#0000FF'>return</font> <font color='#BB00BB'>flows</font><font face='Lucida Console'>(</font>it.idx, it.cnt<font face='Lucida Console'>)</font>;
                    <font color='#0000FF'>else</font> <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>it.cnt <font color='#5555FF'>=</font><font color='#5555FF'>=</font> num<font face='Lucida Console'>)</font>
                        <font color='#0000FF'>return</font> <font color='#BB00BB'>source_flows</font><font face='Lucida Console'>(</font>it.idx,<font color='#979000'>1</font><font face='Lucida Console'>)</font>;
                    <font color='#0000FF'>else</font>
                        <font color='#0000FF'>return</font> <font color='#BB00BB'>sink_flows</font><font face='Lucida Console'>(</font>it.idx,<font color='#979000'>1</font><font face='Lucida Console'>)</font>;
                <b>}</b>
                <font color='#0000FF'>else</font>
                <b>{</b>
                    <font color='#009900'>// if it.idx is the source
</font>                    <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>it.idx <font color='#5555FF'>=</font><font color='#5555FF'>=</font> g.<font color='#BB00BB'>number_of_nodes</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>
                    <b>{</b>
                        <font color='#0000FF'>return</font> <font color='#BB00BB'>source_flows</font><font face='Lucida Console'>(</font>it.cnt,<font color='#979000'>0</font><font face='Lucida Console'>)</font>;
                    <b>}</b>
                    <font color='#0000FF'>else</font> <font color='#009900'>// if it.idx is the sink
</font>                    <b>{</b>
                        <font color='#0000FF'>return</font> <font color='#BB00BB'>sink_flows</font><font face='Lucida Console'>(</font>it.cnt,<font color='#979000'>0</font><font face='Lucida Console'>)</font>;
                    <b>}</b>
                <b>}</b>
            <b>}</b>

            edge_type <b><a name='get_flow'></a>get_flow</b> <font face='Lucida Console'>(</font>
                <font color='#0000FF'>const</font> in_edge_iterator<font color='#5555FF'>&amp;</font> it
            <font face='Lucida Console'>)</font> <font color='#0000FF'>const</font>
            <b>{</b>
                <font color='#0000FF'>return</font> <font color='#BB00BB'>get_flow</font><font face='Lucida Console'>(</font><font color='#BB00BB'>node_id</font><font face='Lucida Console'>(</font>it<font face='Lucida Console'>)</font>, it.idx<font face='Lucida Console'>)</font>; 
            <b>}</b>

            <font color='#0000FF'><u>void</u></font> <b><a name='adjust_flow'></a>adjust_flow</b> <font face='Lucida Console'>(</font>
                <font color='#0000FF'>const</font> <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font><font color='#5555FF'>&amp;</font> it1,     
                <font color='#0000FF'>const</font> <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font><font color='#5555FF'>&amp;</font> it2,     
                <font color='#0000FF'>const</font> edge_type<font color='#5555FF'>&amp;</font> value
            <font face='Lucida Console'>)</font>
            <b>{</b>
                <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>it1 <font color='#5555FF'>&gt;</font><font color='#5555FF'>=</font> g.<font color='#BB00BB'>number_of_nodes</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>
                <b>{</b>
                    <font color='#009900'>// if it1 is the source
</font>                    <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>it1 <font color='#5555FF'>=</font><font color='#5555FF'>=</font> g.<font color='#BB00BB'>number_of_nodes</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>
                    <b>{</b>
                        <font color='#BB00BB'>source_flows</font><font face='Lucida Console'>(</font>it2,<font color='#979000'>0</font><font face='Lucida Console'>)</font> <font color='#5555FF'>+</font><font color='#5555FF'>=</font> value;
                        <font color='#BB00BB'>source_flows</font><font face='Lucida Console'>(</font>it2,<font color='#979000'>1</font><font face='Lucida Console'>)</font> <font color='#5555FF'>-</font><font color='#5555FF'>=</font> value;
                    <b>}</b>
                    <font color='#0000FF'>else</font> <font color='#009900'>// if it1 is the sink
</font>                    <b>{</b>
                        <font color='#BB00BB'>sink_flows</font><font face='Lucida Console'>(</font>it2,<font color='#979000'>0</font><font face='Lucida Console'>)</font> <font color='#5555FF'>+</font><font color='#5555FF'>=</font> value;
                        <font color='#BB00BB'>sink_flows</font><font face='Lucida Console'>(</font>it2,<font color='#979000'>1</font><font face='Lucida Console'>)</font> <font color='#5555FF'>-</font><font color='#5555FF'>=</font> value;
                    <b>}</b>
                <b>}</b>
                <font color='#0000FF'>else</font> <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>it2 <font color='#5555FF'>&gt;</font><font color='#5555FF'>=</font> g.<font color='#BB00BB'>number_of_nodes</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>
                <b>{</b>
                    <font color='#009900'>// if it2 is the source
</font>                    <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>it2 <font color='#5555FF'>=</font><font color='#5555FF'>=</font> g.<font color='#BB00BB'>number_of_nodes</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>
                    <b>{</b>
                        <font color='#BB00BB'>source_flows</font><font face='Lucida Console'>(</font>it1,<font color='#979000'>1</font><font face='Lucida Console'>)</font> <font color='#5555FF'>+</font><font color='#5555FF'>=</font> value;
                        <font color='#BB00BB'>source_flows</font><font face='Lucida Console'>(</font>it1,<font color='#979000'>0</font><font face='Lucida Console'>)</font> <font color='#5555FF'>-</font><font color='#5555FF'>=</font> value;
                    <b>}</b>
                    <font color='#0000FF'>else</font> <font color='#009900'>// if it2 is the sink
</font>                    <b>{</b>
                        <font color='#BB00BB'>sink_flows</font><font face='Lucida Console'>(</font>it1,<font color='#979000'>1</font><font face='Lucida Console'>)</font> <font color='#5555FF'>+</font><font color='#5555FF'>=</font> value;
                        <font color='#BB00BB'>sink_flows</font><font face='Lucida Console'>(</font>it1,<font color='#979000'>0</font><font face='Lucida Console'>)</font> <font color='#5555FF'>-</font><font color='#5555FF'>=</font> value;
                    <b>}</b>
                <b>}</b>
                <font color='#0000FF'>else</font>
                <b>{</b>
                    <font color='#BB00BB'>flows</font><font face='Lucida Console'>(</font>it1, g.<font color='#BB00BB'>get_neighbor_idx</font><font face='Lucida Console'>(</font>it1, it2<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font> <font color='#5555FF'>+</font><font color='#5555FF'>=</font> value;
                    <font color='#BB00BB'>flows</font><font face='Lucida Console'>(</font>it2, g.<font color='#BB00BB'>get_neighbor_idx</font><font face='Lucida Console'>(</font>it2, it1<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font> <font color='#5555FF'>-</font><font color='#5555FF'>=</font> value;
                <b>}</b>

            <b>}</b>

            <font color='#0000FF'><u>void</u></font> <b><a name='set_label'></a>set_label</b> <font face='Lucida Console'>(</font>
                <font color='#0000FF'>const</font> <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font><font color='#5555FF'>&amp;</font> it,
                node_label value
            <font face='Lucida Console'>)</font>
            <b>{</b>
                <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>it <font color='#5555FF'>&lt;</font> g.<font color='#BB00BB'>number_of_nodes</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>
                    g.<font color='#BB00BB'>set_label</font><font face='Lucida Console'>(</font>it, value<font face='Lucida Console'>)</font>;
                <font color='#0000FF'>else</font> <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>it <font color='#5555FF'>=</font><font color='#5555FF'>=</font> g.<font color='#BB00BB'>number_of_nodes</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>
                    source_label <font color='#5555FF'>=</font> value;
                <font color='#0000FF'>else</font> 
                    sink_label <font color='#5555FF'>=</font> value;
            <b>}</b>

            node_label <b><a name='get_label'></a>get_label</b> <font face='Lucida Console'>(</font>
                <font color='#0000FF'>const</font> <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font><font color='#5555FF'>&amp;</font> it
            <font face='Lucida Console'>)</font> <font color='#0000FF'>const</font>
            <b>{</b>
                <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>it <font color='#5555FF'>&lt;</font> g.<font color='#BB00BB'>number_of_nodes</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>
                    <font color='#0000FF'>return</font> g.<font color='#BB00BB'>get_label</font><font face='Lucida Console'>(</font>it<font face='Lucida Console'>)</font>;
                <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>it <font color='#5555FF'>=</font><font color='#5555FF'>=</font> g.<font color='#BB00BB'>number_of_nodes</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>
                    <font color='#0000FF'>return</font> source_label;
                <font color='#0000FF'>else</font>
                    <font color='#0000FF'>return</font> sink_label;
            <b>}</b>

        <b>}</b>;

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
        <font color='#0000FF'>template</font> <font color='#5555FF'>&lt;</font>
            <font color='#0000FF'>typename</font> label_image_type,
            <font color='#0000FF'>typename</font> image_potts_model
            <font color='#5555FF'>&gt;</font>
        <font color='#0000FF'>class</font> <b><a name='potts_grid_problem'></a>potts_grid_problem</b> 
        <b>{</b>
            label_image_type<font color='#5555FF'>&amp;</font> label_img;
            <font color='#0000FF'><u>long</u></font> nc;
            <font color='#0000FF'><u>long</u></font> num_nodes;
            <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>char</u></font><font color='#5555FF'>*</font> labels;
            <font color='#0000FF'>const</font> image_potts_model<font color='#5555FF'>&amp;</font> model;

        <font color='#0000FF'>public</font>:
            <font color='#0000FF'>const</font> <font color='#0000FF'>static</font> <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> max_number_of_neighbors <font color='#5555FF'>=</font> <font color='#979000'>4</font>;

            <b><a name='potts_grid_problem'></a>potts_grid_problem</b> <font face='Lucida Console'>(</font>
                label_image_type<font color='#5555FF'>&amp;</font> label_img_,
                <font color='#0000FF'>const</font> image_potts_model<font color='#5555FF'>&amp;</font> image_potts_model_
            <font face='Lucida Console'>)</font> : 
                label_img<font face='Lucida Console'>(</font>label_img_<font face='Lucida Console'>)</font>,
                model<font face='Lucida Console'>(</font>image_potts_model_<font face='Lucida Console'>)</font>
            <b>{</b>
                num_nodes <font color='#5555FF'>=</font> model.<font color='#BB00BB'>nr</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font color='#5555FF'>*</font>model.<font color='#BB00BB'>nc</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
                nc <font color='#5555FF'>=</font> model.<font color='#BB00BB'>nc</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
                labels <font color='#5555FF'>=</font> <font color='#5555FF'>&amp;</font>label_img[<font color='#979000'>0</font>][<font color='#979000'>0</font>];
            <b>}</b>

            <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> <b><a name='number_of_nodes'></a>number_of_nodes</b> <font face='Lucida Console'>(</font>
            <font face='Lucida Console'>)</font> <font color='#0000FF'>const</font> <b>{</b> <font color='#0000FF'>return</font> num_nodes; <b>}</b>

            <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> <b><a name='number_of_neighbors'></a>number_of_neighbors</b> <font face='Lucida Console'>(</font>
                <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> 
            <font face='Lucida Console'>)</font> <font color='#0000FF'>const</font> 
            <b>{</b> 
                <font color='#0000FF'>return</font> <font color='#979000'>4</font>;
            <b>}</b>

            <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> <b><a name='get_neighbor_idx'></a>get_neighbor_idx</b> <font face='Lucida Console'>(</font>
                <font color='#0000FF'><u>long</u></font> node_id1,
                <font color='#0000FF'><u>long</u></font> node_id2
            <font face='Lucida Console'>)</font> <font color='#0000FF'>const</font>
            <b>{</b>
                <font color='#0000FF'><u>long</u></font> diff <font color='#5555FF'>=</font> node_id2<font color='#5555FF'>-</font>node_id1;
                <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>diff <font color='#5555FF'>&gt;</font> nc<font face='Lucida Console'>)</font>
                    diff <font color='#5555FF'>-</font><font color='#5555FF'>=</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>long</u></font><font face='Lucida Console'>)</font><font color='#BB00BB'>number_of_nodes</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
                <font color='#0000FF'>else</font> <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>diff <font color='#5555FF'>&lt;</font> <font color='#5555FF'>-</font>nc<font face='Lucida Console'>)</font>
                    diff <font color='#5555FF'>+</font><font color='#5555FF'>=</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>long</u></font><font face='Lucida Console'>)</font><font color='#BB00BB'>number_of_nodes</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;

                <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>diff <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>1</font><font face='Lucida Console'>)</font> 
                    <font color='#0000FF'>return</font> <font color='#979000'>0</font>;
                <font color='#0000FF'>else</font> <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>diff <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#5555FF'>-</font><font color='#979000'>1</font><font face='Lucida Console'>)</font>
                    <font color='#0000FF'>return</font> <font color='#979000'>1</font>;
                <font color='#0000FF'>else</font> <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>diff <font color='#5555FF'>=</font><font color='#5555FF'>=</font> nc<font face='Lucida Console'>)</font>
                    <font color='#0000FF'>return</font> <font color='#979000'>2</font>;
                <font color='#0000FF'>else</font>
                    <font color='#0000FF'>return</font> <font color='#979000'>3</font>;
            <b>}</b>

            <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> <b><a name='get_neighbor'></a>get_neighbor</b> <font face='Lucida Console'>(</font>
                <font color='#0000FF'><u>long</u></font> node_id,
                <font color='#0000FF'><u>long</u></font> idx
            <font face='Lucida Console'>)</font> <font color='#0000FF'>const</font>
            <b>{</b>
                <font color='#0000FF'>switch</font><font face='Lucida Console'>(</font>idx<font face='Lucida Console'>)</font>
                <b>{</b>
                    <font color='#0000FF'>case</font> <font color='#979000'>0</font>: 
                        <b>{</b>
                            <font color='#0000FF'><u>long</u></font> temp <font color='#5555FF'>=</font> node_id<font color='#5555FF'>+</font><font color='#979000'>1</font>;
                            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>temp <font color='#5555FF'>&lt;</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>long</u></font><font face='Lucida Console'>)</font><font color='#BB00BB'>number_of_nodes</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>
                                <font color='#0000FF'>return</font> temp;
                            <font color='#0000FF'>else</font>
                                <font color='#0000FF'>return</font> temp <font color='#5555FF'>-</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>long</u></font><font face='Lucida Console'>)</font><font color='#BB00BB'>number_of_nodes</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
                        <b>}</b>
                    <font color='#0000FF'>case</font> <font color='#979000'>1</font>: 
                        <b>{</b>
                            <font color='#0000FF'><u>long</u></font> temp <font color='#5555FF'>=</font> node_id<font color='#5555FF'>-</font><font color='#979000'>1</font>;
                            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>node_id <font color='#5555FF'>&gt;</font><font color='#5555FF'>=</font> <font color='#979000'>1</font><font face='Lucida Console'>)</font>
                                <font color='#0000FF'>return</font> temp;
                            <font color='#0000FF'>else</font>
                                <font color='#0000FF'>return</font> temp <font color='#5555FF'>+</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>long</u></font><font face='Lucida Console'>)</font><font color='#BB00BB'>number_of_nodes</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
                        <b>}</b>
                    <font color='#0000FF'>case</font> <font color='#979000'>2</font>: 
                        <b>{</b>
                            <font color='#0000FF'><u>long</u></font> temp <font color='#5555FF'>=</font> node_id<font color='#5555FF'>+</font>nc;
                            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>temp <font color='#5555FF'>&lt;</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>long</u></font><font face='Lucida Console'>)</font><font color='#BB00BB'>number_of_nodes</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>
                                <font color='#0000FF'>return</font> temp;
                            <font color='#0000FF'>else</font>
                                <font color='#0000FF'>return</font> temp <font color='#5555FF'>-</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>long</u></font><font face='Lucida Console'>)</font><font color='#BB00BB'>number_of_nodes</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
                        <b>}</b>
                    <font color='#0000FF'>case</font> <font color='#979000'>3</font>: 
                        <b>{</b>
                            <font color='#0000FF'><u>long</u></font> temp <font color='#5555FF'>=</font> node_id<font color='#5555FF'>-</font>nc;
                            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>node_id <font color='#5555FF'>&gt;</font><font color='#5555FF'>=</font> nc<font face='Lucida Console'>)</font>
                                <font color='#0000FF'>return</font> temp;
                            <font color='#0000FF'>else</font>
                                <font color='#0000FF'>return</font> temp <font color='#5555FF'>+</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>long</u></font><font face='Lucida Console'>)</font><font color='#BB00BB'>number_of_nodes</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
                        <b>}</b>
                <b>}</b>
                <font color='#0000FF'>return</font> <font color='#979000'>0</font>;
            <b>}</b>

            <font color='#0000FF'><u>void</u></font> <b><a name='set_label'></a>set_label</b> <font face='Lucida Console'>(</font>
                <font color='#0000FF'>const</font> <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font><font color='#5555FF'>&amp;</font> idx,
                node_label value
            <font face='Lucida Console'>)</font>
            <b>{</b>
                <font color='#5555FF'>*</font><font face='Lucida Console'>(</font>labels<font color='#5555FF'>+</font>idx<font face='Lucida Console'>)</font> <font color='#5555FF'>=</font> value;
            <b>}</b>

            node_label <b><a name='get_label'></a>get_label</b> <font face='Lucida Console'>(</font>
                <font color='#0000FF'>const</font> <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font><font color='#5555FF'>&amp;</font> idx
            <font face='Lucida Console'>)</font> <font color='#0000FF'>const</font>
            <b>{</b>
                <font color='#0000FF'>return</font> <font color='#5555FF'>*</font><font face='Lucida Console'>(</font>labels<font color='#5555FF'>+</font>idx<font face='Lucida Console'>)</font>;
            <b>}</b>

            <font color='#0000FF'>typedef</font> <font color='#0000FF'>typename</font> image_potts_model::value_type value_type;

            value_type <b><a name='factor_value'></a>factor_value</b> <font face='Lucida Console'>(</font><font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> idx<font face='Lucida Console'>)</font> <font color='#0000FF'>const</font>
            <b>{</b>
                <font color='#0000FF'>return</font> model.<font color='#BB00BB'>factor_value</font><font face='Lucida Console'>(</font>idx<font face='Lucida Console'>)</font>;
            <b>}</b>

            value_type <b><a name='factor_value_disagreement'></a>factor_value_disagreement</b> <font face='Lucida Console'>(</font><font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> idx1, <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> idx2<font face='Lucida Console'>)</font> <font color='#0000FF'>const</font>
            <b>{</b>
                <font color='#0000FF'>return</font> model.<font color='#BB00BB'>factor_value_disagreement</font><font face='Lucida Console'>(</font>idx1,idx2<font face='Lucida Console'>)</font>;
            <b>}</b>

        <b>}</b>;

    <b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font><font color='#009900'>// ----------------------------------------------------------------------------------------
</font><font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    <font color='#0000FF'>template</font> <font color='#5555FF'>&lt;</font>
        <font color='#0000FF'>typename</font> potts_model
        <font color='#5555FF'>&gt;</font>
    <font color='#0000FF'>typename</font> potts_model::value_type <b><a name='potts_model_score'></a>potts_model_score</b> <font face='Lucida Console'>(</font>
        <font color='#0000FF'>const</font> potts_model<font color='#5555FF'>&amp;</font> prob
    <font face='Lucida Console'>)</font>
    <b>{</b>
<font color='#0000FF'>#ifdef</font> ENABLE_ASSERTS
        <font color='#0000FF'>for</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> i <font color='#5555FF'>=</font> <font color='#979000'>0</font>; i <font color='#5555FF'>&lt;</font> prob.<font color='#BB00BB'>number_of_nodes</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>; <font color='#5555FF'>+</font><font color='#5555FF'>+</font>i<font face='Lucida Console'>)</font>
        <b>{</b>
            <font color='#0000FF'>for</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> jj <font color='#5555FF'>=</font> <font color='#979000'>0</font>; jj <font color='#5555FF'>&lt;</font> prob.<font color='#BB00BB'>number_of_neighbors</font><font face='Lucida Console'>(</font>i<font face='Lucida Console'>)</font>; <font color='#5555FF'>+</font><font color='#5555FF'>+</font>jj<font face='Lucida Console'>)</font>
            <b>{</b>
                <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> j <font color='#5555FF'>=</font> prob.<font color='#BB00BB'>get_neighbor</font><font face='Lucida Console'>(</font>i,jj<font face='Lucida Console'>)</font>;
                <font color='#BB00BB'>DLIB_ASSERT</font><font face='Lucida Console'>(</font>prob.<font color='#BB00BB'>factor_value_disagreement</font><font face='Lucida Console'>(</font>i,j<font face='Lucida Console'>)</font> <font color='#5555FF'>&gt;</font><font color='#5555FF'>=</font> <font color='#979000'>0</font>,
                    "<font color='#CC0000'>\t value_type potts_model_score(prob)</font>"
                    <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\t Invalid inputs were given to this function.</font>" 
                    <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\t i: </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> i 
                    <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\t j: </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> j 
                    <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\t prob.factor_value_disagreement(i,j): </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> prob.<font color='#BB00BB'>factor_value_disagreement</font><font face='Lucida Console'>(</font>i,j<font face='Lucida Console'>)</font>
                    <font face='Lucida Console'>)</font>;
                <font color='#BB00BB'>DLIB_ASSERT</font><font face='Lucida Console'>(</font>prob.<font color='#BB00BB'>factor_value_disagreement</font><font face='Lucida Console'>(</font>i,j<font face='Lucida Console'>)</font> <font color='#5555FF'>=</font><font color='#5555FF'>=</font> prob.<font color='#BB00BB'>factor_value_disagreement</font><font face='Lucida Console'>(</font>j,i<font face='Lucida Console'>)</font>,
                    "<font color='#CC0000'>\t value_type potts_model_score(prob)</font>"
                    <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\t Invalid inputs were given to this function.</font>" 
                    <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\t i: </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> i 
                    <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\t j: </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> j 
                    <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\t prob.factor_value_disagreement(i,j): </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> prob.<font color='#BB00BB'>factor_value_disagreement</font><font face='Lucida Console'>(</font>i,j<font face='Lucida Console'>)</font>
                    <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\t prob.factor_value_disagreement(j,i): </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> prob.<font color='#BB00BB'>factor_value_disagreement</font><font face='Lucida Console'>(</font>j,i<font face='Lucida Console'>)</font>
                    <font face='Lucida Console'>)</font>;
            <b>}</b>
        <b>}</b>
<font color='#0000FF'>#endif</font> 

        <font color='#0000FF'>typename</font> potts_model::value_type score <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
        <font color='#0000FF'>for</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> i <font color='#5555FF'>=</font> <font color='#979000'>0</font>; i <font color='#5555FF'>&lt;</font> prob.<font color='#BB00BB'>number_of_nodes</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>; <font color='#5555FF'>+</font><font color='#5555FF'>+</font>i<font face='Lucida Console'>)</font>
        <b>{</b>
            <font color='#0000FF'>const</font> <font color='#0000FF'><u>bool</u></font> label <font color='#5555FF'>=</font> <font face='Lucida Console'>(</font>prob.<font color='#BB00BB'>get_label</font><font face='Lucida Console'>(</font>i<font face='Lucida Console'>)</font><font color='#5555FF'>!</font><font color='#5555FF'>=</font><font color='#979000'>0</font><font face='Lucida Console'>)</font>;
            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>label<font face='Lucida Console'>)</font>
                score <font color='#5555FF'>+</font><font color='#5555FF'>=</font> prob.<font color='#BB00BB'>factor_value</font><font face='Lucida Console'>(</font>i<font face='Lucida Console'>)</font>;
        <b>}</b>

        <font color='#0000FF'>for</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> i <font color='#5555FF'>=</font> <font color='#979000'>0</font>; i <font color='#5555FF'>&lt;</font> prob.<font color='#BB00BB'>number_of_nodes</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>; <font color='#5555FF'>+</font><font color='#5555FF'>+</font>i<font face='Lucida Console'>)</font>
        <b>{</b>
            <font color='#0000FF'>for</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> n <font color='#5555FF'>=</font> <font color='#979000'>0</font>; n <font color='#5555FF'>&lt;</font> prob.<font color='#BB00BB'>number_of_neighbors</font><font face='Lucida Console'>(</font>i<font face='Lucida Console'>)</font>; <font color='#5555FF'>+</font><font color='#5555FF'>+</font>n<font face='Lucida Console'>)</font>
            <b>{</b>
                <font color='#0000FF'>const</font> <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> idx2 <font color='#5555FF'>=</font> prob.<font color='#BB00BB'>get_neighbor</font><font face='Lucida Console'>(</font>i,n<font face='Lucida Console'>)</font>;
                <font color='#0000FF'>const</font> <font color='#0000FF'><u>bool</u></font> label_i <font color='#5555FF'>=</font> <font face='Lucida Console'>(</font>prob.<font color='#BB00BB'>get_label</font><font face='Lucida Console'>(</font>i<font face='Lucida Console'>)</font><font color='#5555FF'>!</font><font color='#5555FF'>=</font><font color='#979000'>0</font><font face='Lucida Console'>)</font>;
                <font color='#0000FF'>const</font> <font color='#0000FF'><u>bool</u></font> label_idx2 <font color='#5555FF'>=</font> <font face='Lucida Console'>(</font>prob.<font color='#BB00BB'>get_label</font><font face='Lucida Console'>(</font>idx2<font face='Lucida Console'>)</font><font color='#5555FF'>!</font><font color='#5555FF'>=</font><font color='#979000'>0</font><font face='Lucida Console'>)</font>;
                <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>label_i <font color='#5555FF'>!</font><font color='#5555FF'>=</font> label_idx2 <font color='#5555FF'>&amp;</font><font color='#5555FF'>&amp;</font> i <font color='#5555FF'>&lt;</font> idx2<font face='Lucida Console'>)</font>
                    score <font color='#5555FF'>-</font><font color='#5555FF'>=</font> prob.<font color='#BB00BB'>factor_value_disagreement</font><font face='Lucida Console'>(</font>i, idx2<font face='Lucida Console'>)</font>;
            <b>}</b>
        <b>}</b>

        <font color='#0000FF'>return</font> score;
    <b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    <font color='#0000FF'>template</font> <font color='#5555FF'>&lt;</font>
        <font color='#0000FF'>typename</font> graph_type 
        <font color='#5555FF'>&gt;</font>
    <font color='#0000FF'>typename</font> graph_type::edge_type <b><a name='potts_model_score'></a>potts_model_score</b> <font face='Lucida Console'>(</font>
        <font color='#0000FF'>const</font> graph_type<font color='#5555FF'>&amp;</font> g,
        <font color='#0000FF'>const</font> std::vector<font color='#5555FF'>&lt;</font>node_label<font color='#5555FF'>&gt;</font><font color='#5555FF'>&amp;</font> labels
    <font face='Lucida Console'>)</font>
    <b>{</b>
        <font color='#BB00BB'>DLIB_ASSERT</font><font face='Lucida Console'>(</font><font color='#BB00BB'>graph_contains_length_one_cycle</font><font face='Lucida Console'>(</font>g<font face='Lucida Console'>)</font> <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>false</font>,
                    "<font color='#CC0000'>\t edge_type potts_model_score(g,labels)</font>"
                    <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\t Invalid inputs were given to this function.</font>" 
                    <font face='Lucida Console'>)</font>;
        <font color='#0000FF'>typedef</font> <font color='#0000FF'>typename</font> graph_type::edge_type edge_type;
        <font color='#0000FF'>typedef</font> <font color='#0000FF'>typename</font> graph_type::type type;

        <font color='#009900'>// The edges and node's have to use the same type to represent factor weights!
</font>        <font color='#BB00BB'>COMPILE_TIME_ASSERT</font><font face='Lucida Console'>(</font><font face='Lucida Console'>(</font>is_same_type<font color='#5555FF'>&lt;</font>edge_type, type<font color='#5555FF'>&gt;</font>::value <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>true</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;

<font color='#0000FF'>#ifdef</font> ENABLE_ASSERTS
        <font color='#0000FF'>for</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> i <font color='#5555FF'>=</font> <font color='#979000'>0</font>; i <font color='#5555FF'>&lt;</font> g.<font color='#BB00BB'>number_of_nodes</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>; <font color='#5555FF'>+</font><font color='#5555FF'>+</font>i<font face='Lucida Console'>)</font>
        <b>{</b>
            <font color='#0000FF'>for</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> jj <font color='#5555FF'>=</font> <font color='#979000'>0</font>; jj <font color='#5555FF'>&lt;</font> g.<font color='#BB00BB'>node</font><font face='Lucida Console'>(</font>i<font face='Lucida Console'>)</font>.<font color='#BB00BB'>number_of_neighbors</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>; <font color='#5555FF'>+</font><font color='#5555FF'>+</font>jj<font face='Lucida Console'>)</font>
            <b>{</b>
                <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> j <font color='#5555FF'>=</font> g.<font color='#BB00BB'>node</font><font face='Lucida Console'>(</font>i<font face='Lucida Console'>)</font>.<font color='#BB00BB'>neighbor</font><font face='Lucida Console'>(</font>jj<font face='Lucida Console'>)</font>.<font color='#BB00BB'>index</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
                <font color='#BB00BB'>DLIB_ASSERT</font><font face='Lucida Console'>(</font><font color='#BB00BB'>edge</font><font face='Lucida Console'>(</font>g,i,j<font face='Lucida Console'>)</font> <font color='#5555FF'>&gt;</font><font color='#5555FF'>=</font> <font color='#979000'>0</font>,
                    "<font color='#CC0000'>\t edge_type potts_model_score(g,labels)</font>"
                    <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\t Invalid inputs were given to this function.</font>" 
                    <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\t i: </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> i 
                    <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\t j: </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> j 
                    <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\t edge(g,i,j): </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> <font color='#BB00BB'>edge</font><font face='Lucida Console'>(</font>g,i,j<font face='Lucida Console'>)</font>
                    <font face='Lucida Console'>)</font>;
            <b>}</b>
        <b>}</b>
<font color='#0000FF'>#endif</font> 

        <font color='#0000FF'>typename</font> graph_type::edge_type score <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
        <font color='#0000FF'>for</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> i <font color='#5555FF'>=</font> <font color='#979000'>0</font>; i <font color='#5555FF'>&lt;</font> g.<font color='#BB00BB'>number_of_nodes</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>; <font color='#5555FF'>+</font><font color='#5555FF'>+</font>i<font face='Lucida Console'>)</font>
        <b>{</b>
            <font color='#0000FF'>const</font> <font color='#0000FF'><u>bool</u></font> label <font color='#5555FF'>=</font> <font face='Lucida Console'>(</font>labels[i]<font color='#5555FF'>!</font><font color='#5555FF'>=</font><font color='#979000'>0</font><font face='Lucida Console'>)</font>;
            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>label<font face='Lucida Console'>)</font>
                score <font color='#5555FF'>+</font><font color='#5555FF'>=</font> g.<font color='#BB00BB'>node</font><font face='Lucida Console'>(</font>i<font face='Lucida Console'>)</font>.data;
        <b>}</b>

        <font color='#0000FF'>for</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> i <font color='#5555FF'>=</font> <font color='#979000'>0</font>; i <font color='#5555FF'>&lt;</font> g.<font color='#BB00BB'>number_of_nodes</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>; <font color='#5555FF'>+</font><font color='#5555FF'>+</font>i<font face='Lucida Console'>)</font>
        <b>{</b>
            <font color='#0000FF'>for</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> n <font color='#5555FF'>=</font> <font color='#979000'>0</font>; n <font color='#5555FF'>&lt;</font> g.<font color='#BB00BB'>node</font><font face='Lucida Console'>(</font>i<font face='Lucida Console'>)</font>.<font color='#BB00BB'>number_of_neighbors</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>; <font color='#5555FF'>+</font><font color='#5555FF'>+</font>n<font face='Lucida Console'>)</font>
            <b>{</b>
                <font color='#0000FF'>const</font> <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> idx2 <font color='#5555FF'>=</font> g.<font color='#BB00BB'>node</font><font face='Lucida Console'>(</font>i<font face='Lucida Console'>)</font>.<font color='#BB00BB'>neighbor</font><font face='Lucida Console'>(</font>n<font face='Lucida Console'>)</font>.<font color='#BB00BB'>index</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
                <font color='#0000FF'>const</font> <font color='#0000FF'><u>bool</u></font> label_i <font color='#5555FF'>=</font> <font face='Lucida Console'>(</font>labels[i]<font color='#5555FF'>!</font><font color='#5555FF'>=</font><font color='#979000'>0</font><font face='Lucida Console'>)</font>;
                <font color='#0000FF'>const</font> <font color='#0000FF'><u>bool</u></font> label_idx2 <font color='#5555FF'>=</font> <font face='Lucida Console'>(</font>labels[idx2]<font color='#5555FF'>!</font><font color='#5555FF'>=</font><font color='#979000'>0</font><font face='Lucida Console'>)</font>;
                <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>label_i <font color='#5555FF'>!</font><font color='#5555FF'>=</font> label_idx2 <font color='#5555FF'>&amp;</font><font color='#5555FF'>&amp;</font> i <font color='#5555FF'>&lt;</font> idx2<font face='Lucida Console'>)</font>
                    score <font color='#5555FF'>-</font><font color='#5555FF'>=</font> g.<font color='#BB00BB'>node</font><font face='Lucida Console'>(</font>i<font face='Lucida Console'>)</font>.<font color='#BB00BB'>edge</font><font face='Lucida Console'>(</font>n<font face='Lucida Console'>)</font>;
            <b>}</b>
        <b>}</b>

        <font color='#0000FF'>return</font> score;
    <b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    <font color='#0000FF'>template</font> <font color='#5555FF'>&lt;</font>
        <font color='#0000FF'>typename</font> potts_grid_problem,
        <font color='#0000FF'>typename</font> mem_manager
        <font color='#5555FF'>&gt;</font>
    <font color='#0000FF'>typename</font> potts_grid_problem::value_type <b><a name='potts_model_score'></a>potts_model_score</b> <font face='Lucida Console'>(</font>
        <font color='#0000FF'>const</font> potts_grid_problem<font color='#5555FF'>&amp;</font> prob,
        <font color='#0000FF'>const</font> array2d<font color='#5555FF'>&lt;</font>node_label,mem_manager<font color='#5555FF'>&gt;</font><font color='#5555FF'>&amp;</font> labels
    <font face='Lucida Console'>)</font>
    <b>{</b>
        <font color='#BB00BB'>DLIB_ASSERT</font><font face='Lucida Console'>(</font>prob.<font color='#BB00BB'>nr</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>=</font><font color='#5555FF'>=</font> labels.<font color='#BB00BB'>nr</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>&amp;</font><font color='#5555FF'>&amp;</font> prob.<font color='#BB00BB'>nc</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>=</font><font color='#5555FF'>=</font> labels.<font color='#BB00BB'>nc</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>,
            "<font color='#CC0000'>\t value_type potts_model_score(prob,labels)</font>"
            <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\t Invalid inputs were given to this function.</font>" 
            <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\t prob.nr(): </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> labels.<font color='#BB00BB'>nr</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>
            <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\t prob.nc(): </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> labels.<font color='#BB00BB'>nc</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>
            <font face='Lucida Console'>)</font>;
        <font color='#0000FF'>typedef</font> array2d<font color='#5555FF'>&lt;</font>node_label,mem_manager<font color='#5555FF'>&gt;</font> image_type;
        <font color='#009900'>// This const_cast is ok because the model object won't actually modify labels
</font>        dlib::impl::potts_grid_problem<font color='#5555FF'>&lt;</font>image_type,potts_grid_problem<font color='#5555FF'>&gt;</font> <font color='#BB00BB'>model</font><font face='Lucida Console'>(</font><font color='#0000FF'>const_cast</font><font color='#5555FF'>&lt;</font>image_type<font color='#5555FF'>&amp;</font><font color='#5555FF'>&gt;</font><font face='Lucida Console'>(</font>labels<font face='Lucida Console'>)</font>,prob<font face='Lucida Console'>)</font>;
        <font color='#0000FF'>return</font> <font color='#BB00BB'>potts_model_score</font><font face='Lucida Console'>(</font>model<font face='Lucida Console'>)</font>;
    <b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    <font color='#0000FF'>template</font> <font color='#5555FF'>&lt;</font>
        <font color='#0000FF'>typename</font> potts_model
        <font color='#5555FF'>&gt;</font>
    <font color='#0000FF'><u>void</u></font> <b><a name='find_max_factor_graph_potts'></a>find_max_factor_graph_potts</b> <font face='Lucida Console'>(</font>
        potts_model<font color='#5555FF'>&amp;</font> prob
    <font face='Lucida Console'>)</font>
    <b>{</b>
<font color='#0000FF'>#ifdef</font> ENABLE_ASSERTS
        <font color='#0000FF'>for</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> node_i <font color='#5555FF'>=</font> <font color='#979000'>0</font>; node_i <font color='#5555FF'>&lt;</font> prob.<font color='#BB00BB'>number_of_nodes</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>; <font color='#5555FF'>+</font><font color='#5555FF'>+</font>node_i<font face='Lucida Console'>)</font>
        <b>{</b>
            <font color='#0000FF'>for</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> jj <font color='#5555FF'>=</font> <font color='#979000'>0</font>; jj <font color='#5555FF'>&lt;</font> prob.<font color='#BB00BB'>number_of_neighbors</font><font face='Lucida Console'>(</font>node_i<font face='Lucida Console'>)</font>; <font color='#5555FF'>+</font><font color='#5555FF'>+</font>jj<font face='Lucida Console'>)</font>
            <b>{</b>
                <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> node_j <font color='#5555FF'>=</font> prob.<font color='#BB00BB'>get_neighbor</font><font face='Lucida Console'>(</font>node_i,jj<font face='Lucida Console'>)</font>;
                <font color='#BB00BB'>DLIB_ASSERT</font><font face='Lucida Console'>(</font>prob.<font color='#BB00BB'>get_neighbor_idx</font><font face='Lucida Console'>(</font>node_j,node_i<font face='Lucida Console'>)</font> <font color='#5555FF'>&lt;</font> prob.<font color='#BB00BB'>number_of_neighbors</font><font face='Lucida Console'>(</font>node_j<font face='Lucida Console'>)</font>,
                    "<font color='#CC0000'>\t void find_max_factor_graph_potts(prob)</font>"
                    <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\t The supplied potts problem defines an invalid graph.</font>" 
                    <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\t node_i: </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> node_i 
                    <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\t node_j: </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> node_j 
                    <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\t prob.get_neighbor_idx(node_j,node_i): </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> prob.<font color='#BB00BB'>get_neighbor_idx</font><font face='Lucida Console'>(</font>node_j,node_i<font face='Lucida Console'>)</font>
                    <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\t prob.number_of_neighbors(node_j):     </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> prob.<font color='#BB00BB'>number_of_neighbors</font><font face='Lucida Console'>(</font>node_j<font face='Lucida Console'>)</font>
                            <font face='Lucida Console'>)</font>;

                <font color='#BB00BB'>DLIB_ASSERT</font><font face='Lucida Console'>(</font>prob.<font color='#BB00BB'>get_neighbor_idx</font><font face='Lucida Console'>(</font>node_i,prob.<font color='#BB00BB'>get_neighbor</font><font face='Lucida Console'>(</font>node_i,jj<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font> <font color='#5555FF'>=</font><font color='#5555FF'>=</font> jj,
                    "<font color='#CC0000'>\t void find_max_factor_graph_potts(prob)</font>"
                    <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\t The get_neighbor_idx() and get_neighbor() functions must be inverses of each other.</font>" 
                    <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\t node_i: </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> node_i 
                    <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\t jj:     </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> jj
                    <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\t prob.get_neighbor(node_i,jj): </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> prob.<font color='#BB00BB'>get_neighbor</font><font face='Lucida Console'>(</font>node_i,jj<font face='Lucida Console'>)</font>
                    <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\t prob.get_neighbor_idx(node_i,prob.get_neighbor(node_i,jj)): </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> prob.<font color='#BB00BB'>get_neighbor_idx</font><font face='Lucida Console'>(</font>node_i,node_j<font face='Lucida Console'>)</font>
                            <font face='Lucida Console'>)</font>;

                <font color='#BB00BB'>DLIB_ASSERT</font><font face='Lucida Console'>(</font>prob.<font color='#BB00BB'>get_neighbor</font><font face='Lucida Console'>(</font>node_j,prob.<font color='#BB00BB'>get_neighbor_idx</font><font face='Lucida Console'>(</font>node_j,node_i<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font><font color='#5555FF'>=</font><font color='#5555FF'>=</font>node_i,
                    "<font color='#CC0000'>\t void find_max_factor_graph_potts(prob)</font>"
                    <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\t The get_neighbor_idx() and get_neighbor() functions must be inverses of each other.</font>" 
                    <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\t node_i: </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> node_i 
                    <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\t node_j: </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> node_j 
                    <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\t prob.get_neighbor_idx(node_j,node_i): </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> prob.<font color='#BB00BB'>get_neighbor_idx</font><font face='Lucida Console'>(</font>node_j,node_i<font face='Lucida Console'>)</font>
                    <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\t prob.get_neighbor(node_j,prob.get_neighbor_idx(node_j,node_i)): </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> prob.<font color='#BB00BB'>get_neighbor</font><font face='Lucida Console'>(</font>node_j,prob.<font color='#BB00BB'>get_neighbor_idx</font><font face='Lucida Console'>(</font>node_j,node_i<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>
                            <font face='Lucida Console'>)</font>;

                <font color='#BB00BB'>DLIB_ASSERT</font><font face='Lucida Console'>(</font>prob.<font color='#BB00BB'>factor_value_disagreement</font><font face='Lucida Console'>(</font>node_i,node_j<font face='Lucida Console'>)</font> <font color='#5555FF'>&gt;</font><font color='#5555FF'>=</font> <font color='#979000'>0</font>,
                    "<font color='#CC0000'>\t void find_max_factor_graph_potts(prob)</font>"
                    <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\t Invalid inputs were given to this function.</font>" 
                    <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\t node_i: </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> node_i 
                    <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\t node_j: </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> node_j 
                    <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\t prob.factor_value_disagreement(node_i,node_j): </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> prob.<font color='#BB00BB'>factor_value_disagreement</font><font face='Lucida Console'>(</font>node_i,node_j<font face='Lucida Console'>)</font>
                    <font face='Lucida Console'>)</font>;
                <font color='#BB00BB'>DLIB_ASSERT</font><font face='Lucida Console'>(</font>prob.<font color='#BB00BB'>factor_value_disagreement</font><font face='Lucida Console'>(</font>node_i,node_j<font face='Lucida Console'>)</font> <font color='#5555FF'>=</font><font color='#5555FF'>=</font> prob.<font color='#BB00BB'>factor_value_disagreement</font><font face='Lucida Console'>(</font>node_j,node_i<font face='Lucida Console'>)</font>,
                    "<font color='#CC0000'>\t void find_max_factor_graph_potts(prob)</font>"
                    <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\t Invalid inputs were given to this function.</font>" 
                    <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\t node_i: </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> node_i 
                    <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\t node_j: </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> node_j 
                    <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\t prob.factor_value_disagreement(node_i,node_j): </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> prob.<font color='#BB00BB'>factor_value_disagreement</font><font face='Lucida Console'>(</font>node_i,node_j<font face='Lucida Console'>)</font>
                    <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\t prob.factor_value_disagreement(node_j,node_i): </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> prob.<font color='#BB00BB'>factor_value_disagreement</font><font face='Lucida Console'>(</font>node_j,node_i<font face='Lucida Console'>)</font>
                    <font face='Lucida Console'>)</font>;
            <b>}</b>
        <b>}</b>
<font color='#0000FF'>#endif</font> 
        <font color='#BB00BB'>COMPILE_TIME_ASSERT</font><font face='Lucida Console'>(</font>is_signed_type<font color='#5555FF'>&lt;</font><font color='#0000FF'>typename</font> potts_model::value_type<font color='#5555FF'>&gt;</font>::value<font face='Lucida Console'>)</font>;
        min_cut mc;
        dlib::impl::potts_flow_graph<font color='#5555FF'>&lt;</font>potts_model<font color='#5555FF'>&gt;</font> <font color='#BB00BB'>pfg</font><font face='Lucida Console'>(</font>prob<font face='Lucida Console'>)</font>;
        <font color='#BB00BB'>mc</font><font face='Lucida Console'>(</font>pfg, prob.<font color='#BB00BB'>number_of_nodes</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>, prob.<font color='#BB00BB'>number_of_nodes</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font color='#5555FF'>+</font><font color='#979000'>1</font><font face='Lucida Console'>)</font>;
    <b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    <font color='#0000FF'>template</font> <font color='#5555FF'>&lt;</font>
        <font color='#0000FF'>typename</font> graph_type 
        <font color='#5555FF'>&gt;</font>
    <font color='#0000FF'><u>void</u></font> <b><a name='find_max_factor_graph_potts'></a>find_max_factor_graph_potts</b> <font face='Lucida Console'>(</font>
        <font color='#0000FF'>const</font> graph_type<font color='#5555FF'>&amp;</font> g,
        std::vector<font color='#5555FF'>&lt;</font>node_label<font color='#5555FF'>&gt;</font><font color='#5555FF'>&amp;</font> labels
    <font face='Lucida Console'>)</font>
    <b>{</b>
        <font color='#BB00BB'>DLIB_ASSERT</font><font face='Lucida Console'>(</font><font color='#BB00BB'>graph_contains_length_one_cycle</font><font face='Lucida Console'>(</font>g<font face='Lucida Console'>)</font> <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>false</font>,
                    "<font color='#CC0000'>\t void find_max_factor_graph_potts(g,labels)</font>"
                    <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\t Invalid inputs were given to this function.</font>" 
                    <font face='Lucida Console'>)</font>;
        <font color='#0000FF'>typedef</font> <font color='#0000FF'>typename</font> graph_type::edge_type edge_type;
        <font color='#0000FF'>typedef</font> <font color='#0000FF'>typename</font> graph_type::type type;

        <font color='#009900'>// The edges and node's have to use the same type to represent factor weights!
</font>        <font color='#BB00BB'>COMPILE_TIME_ASSERT</font><font face='Lucida Console'>(</font><font face='Lucida Console'>(</font>is_same_type<font color='#5555FF'>&lt;</font>edge_type, type<font color='#5555FF'>&gt;</font>::value <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>true</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
        <font color='#BB00BB'>COMPILE_TIME_ASSERT</font><font face='Lucida Console'>(</font>is_signed_type<font color='#5555FF'>&lt;</font>edge_type<font color='#5555FF'>&gt;</font>::value<font face='Lucida Console'>)</font>;

<font color='#0000FF'>#ifdef</font> ENABLE_ASSERTS
        <font color='#0000FF'>for</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> i <font color='#5555FF'>=</font> <font color='#979000'>0</font>; i <font color='#5555FF'>&lt;</font> g.<font color='#BB00BB'>number_of_nodes</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>; <font color='#5555FF'>+</font><font color='#5555FF'>+</font>i<font face='Lucida Console'>)</font>
        <b>{</b>
            <font color='#0000FF'>for</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> jj <font color='#5555FF'>=</font> <font color='#979000'>0</font>; jj <font color='#5555FF'>&lt;</font> g.<font color='#BB00BB'>node</font><font face='Lucida Console'>(</font>i<font face='Lucida Console'>)</font>.<font color='#BB00BB'>number_of_neighbors</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>; <font color='#5555FF'>+</font><font color='#5555FF'>+</font>jj<font face='Lucida Console'>)</font>
            <b>{</b>
                <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> j <font color='#5555FF'>=</font> g.<font color='#BB00BB'>node</font><font face='Lucida Console'>(</font>i<font face='Lucida Console'>)</font>.<font color='#BB00BB'>neighbor</font><font face='Lucida Console'>(</font>jj<font face='Lucida Console'>)</font>.<font color='#BB00BB'>index</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
                <font color='#BB00BB'>DLIB_ASSERT</font><font face='Lucida Console'>(</font><font color='#BB00BB'>edge</font><font face='Lucida Console'>(</font>g,i,j<font face='Lucida Console'>)</font> <font color='#5555FF'>&gt;</font><font color='#5555FF'>=</font> <font color='#979000'>0</font>,
                    "<font color='#CC0000'>\t void find_max_factor_graph_potts(g,labels)</font>"
                    <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\t Invalid inputs were given to this function.</font>" 
                    <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\t i: </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> i 
                    <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\t j: </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> j 
                    <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\t edge(g,i,j): </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> <font color='#BB00BB'>edge</font><font face='Lucida Console'>(</font>g,i,j<font face='Lucida Console'>)</font>
                    <font face='Lucida Console'>)</font>;
            <b>}</b>
        <b>}</b>
<font color='#0000FF'>#endif</font> 

        dlib::impl::general_potts_problem<font color='#5555FF'>&lt;</font>graph_type<font color='#5555FF'>&gt;</font> <font color='#BB00BB'>gg</font><font face='Lucida Console'>(</font>g, labels<font face='Lucida Console'>)</font>;
        <font color='#BB00BB'>find_max_factor_graph_potts</font><font face='Lucida Console'>(</font>gg<font face='Lucida Console'>)</font>;

    <b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    <font color='#0000FF'>template</font> <font color='#5555FF'>&lt;</font>
        <font color='#0000FF'>typename</font> potts_grid_problem,
        <font color='#0000FF'>typename</font> mem_manager
        <font color='#5555FF'>&gt;</font>
    <font color='#0000FF'><u>void</u></font> <b><a name='find_max_factor_graph_potts'></a>find_max_factor_graph_potts</b> <font face='Lucida Console'>(</font>
        <font color='#0000FF'>const</font> potts_grid_problem<font color='#5555FF'>&amp;</font> prob,
        array2d<font color='#5555FF'>&lt;</font>node_label,mem_manager<font color='#5555FF'>&gt;</font><font color='#5555FF'>&amp;</font> labels
    <font face='Lucida Console'>)</font>
    <b>{</b>
        <font color='#0000FF'>typedef</font> array2d<font color='#5555FF'>&lt;</font>node_label,mem_manager<font color='#5555FF'>&gt;</font> image_type;
        labels.<font color='#BB00BB'>set_size</font><font face='Lucida Console'>(</font>prob.<font color='#BB00BB'>nr</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>, prob.<font color='#BB00BB'>nc</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
        dlib::impl::potts_grid_problem<font color='#5555FF'>&lt;</font>image_type,potts_grid_problem<font color='#5555FF'>&gt;</font> <font color='#BB00BB'>model</font><font face='Lucida Console'>(</font>labels,prob<font face='Lucida Console'>)</font>;
        <font color='#BB00BB'>find_max_factor_graph_potts</font><font face='Lucida Console'>(</font>model<font face='Lucida Console'>)</font>;
    <b>}</b>

<font color='#009900'>// ---------------------------------------------------------------------------------------- 
</font>
    <font color='#0000FF'>namespace</font> impl
    <b>{</b>
        <font color='#0000FF'>template</font> <font color='#5555FF'>&lt;</font>
            <font color='#0000FF'>typename</font> pixel_type1,
            <font color='#0000FF'>typename</font> pixel_type2,
            <font color='#0000FF'>typename</font> model_type
            <font color='#5555FF'>&gt;</font>
        <font color='#0000FF'>struct</font> <b><a name='potts_grid_image_pair_model'></a>potts_grid_image_pair_model</b>
        <b>{</b>
            <font color='#0000FF'>const</font> pixel_type1<font color='#5555FF'>*</font> data1;
            <font color='#0000FF'>const</font> pixel_type2<font color='#5555FF'>*</font> data2;
            <font color='#0000FF'>const</font> model_type<font color='#5555FF'>&amp;</font> model;
            <font color='#0000FF'>const</font> <font color='#0000FF'><u>long</u></font> nr_;
            <font color='#0000FF'>const</font> <font color='#0000FF'><u>long</u></font> nc_;
            <font color='#0000FF'>template</font> <font color='#5555FF'>&lt;</font><font color='#0000FF'>typename</font> image_type1, <font color='#0000FF'>typename</font> image_type2<font color='#5555FF'>&gt;</font>
            <b><a name='potts_grid_image_pair_model'></a>potts_grid_image_pair_model</b><font face='Lucida Console'>(</font>
                <font color='#0000FF'>const</font> model_type<font color='#5555FF'>&amp;</font> model_,
                <font color='#0000FF'>const</font> image_type1<font color='#5555FF'>&amp;</font> img1,
                <font color='#0000FF'>const</font> image_type2<font color='#5555FF'>&amp;</font> img2
            <font face='Lucida Console'>)</font> :
                model<font face='Lucida Console'>(</font>model_<font face='Lucida Console'>)</font>,
                nr_<font face='Lucida Console'>(</font>img1.nr<font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>,
                nc_<font face='Lucida Console'>(</font>img1.nc<font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>
            <b>{</b>
                data1 <font color='#5555FF'>=</font> <font color='#5555FF'>&amp;</font>img1[<font color='#979000'>0</font>][<font color='#979000'>0</font>];
                data2 <font color='#5555FF'>=</font> <font color='#5555FF'>&amp;</font>img2[<font color='#979000'>0</font>][<font color='#979000'>0</font>];
            <b>}</b>

            <font color='#0000FF'>typedef</font> <font color='#0000FF'>typename</font> model_type::value_type value_type;

            <font color='#0000FF'><u>long</u></font> <b><a name='nr'></a>nr</b><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#0000FF'>const</font> <b>{</b> <font color='#0000FF'>return</font> nr_; <b>}</b>
            <font color='#0000FF'><u>long</u></font> <b><a name='nc'></a>nc</b><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#0000FF'>const</font> <b>{</b> <font color='#0000FF'>return</font> nc_; <b>}</b>

            value_type <b><a name='factor_value'></a>factor_value</b> <font face='Lucida Console'>(</font>
                <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> idx
            <font face='Lucida Console'>)</font> <font color='#0000FF'>const</font> 
            <b>{</b>
                <font color='#0000FF'>return</font> model.<font color='#BB00BB'>factor_value</font><font face='Lucida Console'>(</font><font color='#5555FF'>*</font><font face='Lucida Console'>(</font>data1 <font color='#5555FF'>+</font> idx<font face='Lucida Console'>)</font>, <font color='#5555FF'>*</font><font face='Lucida Console'>(</font>data2 <font color='#5555FF'>+</font> idx<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
            <b>}</b>

            value_type <b><a name='factor_value_disagreement'></a>factor_value_disagreement</b> <font face='Lucida Console'>(</font>
                <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> idx1,
                <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> idx2
            <font face='Lucida Console'>)</font> <font color='#0000FF'>const</font> 
            <b>{</b>
                <font color='#0000FF'>return</font> model.<font color='#BB00BB'>factor_value_disagreement</font><font face='Lucida Console'>(</font><font color='#5555FF'>*</font><font face='Lucida Console'>(</font>data1 <font color='#5555FF'>+</font> idx1<font face='Lucida Console'>)</font>, <font color='#5555FF'>*</font><font face='Lucida Console'>(</font>data1 <font color='#5555FF'>+</font> idx2<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
            <b>}</b>
        <b>}</b>;

    <font color='#009900'>// ---------------------------------------------------------------------------------------- 
</font>
        <font color='#0000FF'>template</font> <font color='#5555FF'>&lt;</font>
            <font color='#0000FF'>typename</font> image_type,
            <font color='#0000FF'>typename</font> model_type
            <font color='#5555FF'>&gt;</font>
        <font color='#0000FF'>struct</font> <b><a name='potts_grid_image_single_model'></a>potts_grid_image_single_model</b>
        <b>{</b>
            <font color='#0000FF'>const</font> <font color='#0000FF'>typename</font> image_type::type<font color='#5555FF'>*</font> data1;
            <font color='#0000FF'>const</font> model_type<font color='#5555FF'>&amp;</font> model;
            <font color='#0000FF'>const</font> <font color='#0000FF'><u>long</u></font> nr_;
            <font color='#0000FF'>const</font> <font color='#0000FF'><u>long</u></font> nc_;
            <b><a name='potts_grid_image_single_model'></a>potts_grid_image_single_model</b><font face='Lucida Console'>(</font>
                <font color='#0000FF'>const</font> model_type<font color='#5555FF'>&amp;</font> model_,
                <font color='#0000FF'>const</font> image_type<font color='#5555FF'>&amp;</font> img1
            <font face='Lucida Console'>)</font> :
                model<font face='Lucida Console'>(</font>model_<font face='Lucida Console'>)</font>,
                nr_<font face='Lucida Console'>(</font>img1.nr<font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>,
                nc_<font face='Lucida Console'>(</font>img1.nc<font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>
            <b>{</b>
                data1 <font color='#5555FF'>=</font> <font color='#5555FF'>&amp;</font>img1[<font color='#979000'>0</font>][<font color='#979000'>0</font>];
            <b>}</b>

            <font color='#0000FF'>typedef</font> <font color='#0000FF'>typename</font> model_type::value_type value_type;

            <font color='#0000FF'><u>long</u></font> <b><a name='nr'></a>nr</b><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#0000FF'>const</font> <b>{</b> <font color='#0000FF'>return</font> nr_; <b>}</b>
            <font color='#0000FF'><u>long</u></font> <b><a name='nc'></a>nc</b><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#0000FF'>const</font> <b>{</b> <font color='#0000FF'>return</font> nc_; <b>}</b>

            value_type <b><a name='factor_value'></a>factor_value</b> <font face='Lucida Console'>(</font>
                <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> idx
            <font face='Lucida Console'>)</font> <font color='#0000FF'>const</font> 
            <b>{</b>
                <font color='#0000FF'>return</font> model.<font color='#BB00BB'>factor_value</font><font face='Lucida Console'>(</font><font color='#5555FF'>*</font><font face='Lucida Console'>(</font>data1 <font color='#5555FF'>+</font> idx<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
            <b>}</b>

            value_type <b><a name='factor_value_disagreement'></a>factor_value_disagreement</b> <font face='Lucida Console'>(</font>
                <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> idx1,
                <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> idx2
            <font face='Lucida Console'>)</font> <font color='#0000FF'>const</font> 
            <b>{</b>
                <font color='#0000FF'>return</font> model.<font color='#BB00BB'>factor_value_disagreement</font><font face='Lucida Console'>(</font><font color='#5555FF'>*</font><font face='Lucida Console'>(</font>data1 <font color='#5555FF'>+</font> idx1<font face='Lucida Console'>)</font>, <font color='#5555FF'>*</font><font face='Lucida Console'>(</font>data1 <font color='#5555FF'>+</font> idx2<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
            <b>}</b>
        <b>}</b>;

    <b>}</b>

<font color='#009900'>// ---------------------------------------------------------------------------------------- 
</font>
    <font color='#0000FF'>template</font> <font color='#5555FF'>&lt;</font>
        <font color='#0000FF'>typename</font> pair_image_model,
        <font color='#0000FF'>typename</font> pixel_type1,
        <font color='#0000FF'>typename</font> pixel_type2,
        <font color='#0000FF'>typename</font> mem_manager
        <font color='#5555FF'>&gt;</font>
    impl::potts_grid_image_pair_model<font color='#5555FF'>&lt;</font>pixel_type1, pixel_type2, pair_image_model<font color='#5555FF'>&gt;</font> <b><a name='make_potts_grid_problem'></a>make_potts_grid_problem</b> <font face='Lucida Console'>(</font>
        <font color='#0000FF'>const</font> pair_image_model<font color='#5555FF'>&amp;</font> model,
        <font color='#0000FF'>const</font> array2d<font color='#5555FF'>&lt;</font>pixel_type1,mem_manager<font color='#5555FF'>&gt;</font><font color='#5555FF'>&amp;</font> img1,
        <font color='#0000FF'>const</font> array2d<font color='#5555FF'>&lt;</font>pixel_type2,mem_manager<font color='#5555FF'>&gt;</font><font color='#5555FF'>&amp;</font> img2
    <font face='Lucida Console'>)</font>
    <b>{</b>
        <font color='#BB00BB'>DLIB_ASSERT</font><font face='Lucida Console'>(</font><font color='#BB00BB'>get_rect</font><font face='Lucida Console'>(</font>img1<font face='Lucida Console'>)</font> <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#BB00BB'>get_rect</font><font face='Lucida Console'>(</font>img2<font face='Lucida Console'>)</font>,
            "<font color='#CC0000'>\t potts_grid_problem make_potts_grid_problem()</font>"
            <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\t Invalid inputs were given to this function.</font>" 
            <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\t get_rect(img1): </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> <font color='#BB00BB'>get_rect</font><font face='Lucida Console'>(</font>img1<font face='Lucida Console'>)</font>
            <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\t get_rect(img2): </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> <font color='#BB00BB'>get_rect</font><font face='Lucida Console'>(</font>img2<font face='Lucida Console'>)</font>
            <font face='Lucida Console'>)</font>;
        <font color='#0000FF'>typedef</font> impl::potts_grid_image_pair_model<font color='#5555FF'>&lt;</font>pixel_type1, pixel_type2, pair_image_model<font color='#5555FF'>&gt;</font> potts_type;
        <font color='#0000FF'>return</font> <font color='#BB00BB'>potts_type</font><font face='Lucida Console'>(</font>model,img1,img2<font face='Lucida Console'>)</font>;
    <b>}</b>

<font color='#009900'>// ---------------------------------------------------------------------------------------- 
</font>
    <font color='#0000FF'>template</font> <font color='#5555FF'>&lt;</font>
        <font color='#0000FF'>typename</font> single_image_model,
        <font color='#0000FF'>typename</font> pixel_type,
        <font color='#0000FF'>typename</font> mem_manager
        <font color='#5555FF'>&gt;</font>
    impl::potts_grid_image_single_model<font color='#5555FF'>&lt;</font>array2d<font color='#5555FF'>&lt;</font>pixel_type,mem_manager<font color='#5555FF'>&gt;</font>, single_image_model<font color='#5555FF'>&gt;</font> <b><a name='make_potts_grid_problem'></a>make_potts_grid_problem</b> <font face='Lucida Console'>(</font>
        <font color='#0000FF'>const</font> single_image_model<font color='#5555FF'>&amp;</font> model,
        <font color='#0000FF'>const</font> array2d<font color='#5555FF'>&lt;</font>pixel_type,mem_manager<font color='#5555FF'>&gt;</font><font color='#5555FF'>&amp;</font> img
    <font face='Lucida Console'>)</font>
    <b>{</b>
        <font color='#0000FF'>typedef</font> impl::potts_grid_image_single_model<font color='#5555FF'>&lt;</font>array2d<font color='#5555FF'>&lt;</font>pixel_type,mem_manager<font color='#5555FF'>&gt;</font>, single_image_model<font color='#5555FF'>&gt;</font> potts_type;
        <font color='#0000FF'>return</font> <font color='#BB00BB'>potts_type</font><font face='Lucida Console'>(</font>model,img<font face='Lucida Console'>)</font>;
    <b>}</b>

<font color='#009900'>// ---------------------------------------------------------------------------------------- 
</font>
<b>}</b>

<font color='#0000FF'>#endif</font> <font color='#009900'>// DLIB_FIND_MAX_FACTOR_GRAPH_PoTTS_Hh_
</font>

</pre></body></html>